package fr.babostesting.examples.mobile.views.app 
{
	import flash.events.TouchEvent;
	import fr.babos.core.actions.Action;
	import fr.babos.core.actions.IAction;
	import fr.babos.core.bootstrap.BootstrapAction;
	import fr.babos.graphic.components.base.BaseComponent;
	import fr.babos.graphic.components.base.ComponentView;
	import fr.babos.graphic.components.base.ResizableComponent;
	import fr.babos.graphic.components.containers.stacks.ActionStack;
	import fr.babos.graphic.components.containers.stacks.StackLayerItem;
	import fr.babos.graphic.components.containers.stacks.ViewStack;
	import fr.babostesting.examples.mobile.actions.ExampleActions;
	
	/**
	 * @author ZoulouX
	 */
	public class MExampleAppView extends ComponentView 
	{
		/**
		 * La stack
		 */
		protected var _stack					:ActionStack;
		
		/**
		 * Le constructeur
		 */
		public function MExampleAppView ()
		{
			trace("MExampleAppView{}");
			
			super();
		}
		
		/**
		 * Initialisation de la vue
		 */
		override public function init ():void
		{
			// Initialiser la stack
			initStack();
			initButtons();
		}
		
		/**
		 * Initialiser la stack
		 */
		protected function initStack ():void
		{
			// Créer la viewStack
			_stack = new ActionStack(_bundle.bootstrap, [
				{
					action: new BootstrapAction(ExampleActions.HOME_ACTION),
					destructionPolicy: StackLayerItem.REMOVE_POLICY
				},
				{
					action: new BootstrapAction(ExampleActions.MENTIONS_ACTION),
					destructionPolicy: StackLayerItem.HIDE_POLICY
				},
				{
					action: new BootstrapAction(ExampleActions.TRENDS_ACTION),
					destructionPolicy: StackLayerItem.REMOVE_POLICY
				},
				{
					action: new BootstrapAction(ExampleActions.SEARCH_ACTION),
					destructionPolicy: StackLayerItem.HIDE_POLICY
				}
			], 0);
			
			// Placer la viewStack
			_stack.place(0, 0, 60, 0).into(resizableComponent);
		}
		
		/**
		 * Initialiser les boutons du menu
		 */
		protected function initButtons ():void
		{
			// Créer un bouton par action
			var button:ResizableComponent;
			const total:uint = _stack.layers.length;
			for (var i:int = 0; i < total; i++) 
			{
				// Créer
				button = new ResizableComponent();
				
				// Dimensions
				button.size(40, 40);
				
				// Placement
				button.place(NaN, NaN, 10, 10 + i * 50);
				
				// Look and feel
				button.graphics.beginFill(0x000000, 0.5);
				button.graphics.drawRoundRect(0, 0, 40, 40, 7, 7);
				button.graphics.endFill();
				
				// Ajout
				button.into(resizableComponent);
				
				// Lui passer son index
				button.index = i;
				
				// Créer l'action bootstrap de ce bouton
				/*
				button.data = new BootstrapAction(_actions[i], {}, {
					displayContainer: _stack
				});
				*/
				
				// Ecouter les clics
				button.addEventListener(TouchEvent.TOUCH_TAP, buttonTapHandler);
			}
		}
		
		/**
		 * Tap sur un bouton du menu
		 */
		protected function buttonTapHandler (event:TouchEvent):void 
		{
			// Lancer l'action sur le bootstrap
			//_bundle.bootstrap.doAction((event.currentTarget as BaseComponent).data as IAction);
			
			// Séléctionner l'index sur la stack
			_stack.selectedIndex = (event.currentTarget as BaseComponent).index;
		}
	}
}